package com.muapet.service.impl;

import com.muapet.mapper.AccountMapper;
import com.muapet.mapper.UserMapper;
import com.muapet.model.Account;
import com.muapet.model.User;
import com.muapet.model.other.Pagination;
import com.muapet.service.IUserService;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

	private UserMapper userMapper;
	private AccountMapper accountMapper;

	@Autowired
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	@Autowired
	public void setAccountMapper(AccountMapper accountMapper) {
		this.accountMapper = accountMapper;
	}

	@Override
	@Transactional(readOnly = true)
	public User login(String loginname, String password) {
		User user = new User();
		user.setLoginname(loginname);
		user.setPassword(password);
		User loginUser = userMapper.selectOne(user);
		if (loginUser != null) {
			loginUser.setLastLoginTime(new Date());
			userMapper.updateByPrimaryKeySelective(loginUser);
		}
		return loginUser;
	}

	@Override
	public List<User> findAll(Pagination pagination) {
		return userMapper.selectByExampleAndRowBounds(null, new RowBounds(
				pagination.getOffset(), pagination.getLimit()));
	}

	@Override
	public Integer countAll() {
		return userMapper.selectCount(null);
	}

	@Override
	public User findById(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<User> findByLoginname(String loginname, Pagination pagination) {
		Example userExample = new Example(User.class);
		userExample.createCriteria()
				.andLike("loginname", "%" + loginname + "%");
		return userMapper.selectByExampleAndRowBounds(userExample,
				new RowBounds(pagination.getOffset(), pagination.getLimit()));
	}

	@Override
	public Integer countByLoginname(String loginname) {
		Example userExample = new Example(User.class);
		userExample.createCriteria()
				.andLike("loginname", "%" + loginname + "%");
		return userMapper.selectCountByExample(userExample);
	}

	@Override
	public User add(User user) {
		if (!checkSameLoginname(user.getLoginname())) {
			return user;
		}
		user = setUserDefaultParam(user);
		userMapper.insertSelective(user);
		return user;
	}

	/**
	 * 设置默认值
	 * 
	 * @param user
	 * @return
	 */
	private User setUserDefaultParam(User user) {
		if (user.getCardId() != null && user.getCardName() != null
				&& !user.getCardId().isEmpty() && !user.getCardName().isEmpty()) {
			user.setIspass(User.USER_ISPASS_WAITING);
		}
		user.setLastLoginTime(new Date());
		user.setRegisterDate(new Date());
		return user;
	}

	@Override
	public boolean checkSameLoginname(String loginname) {
		Example userExample = new Example(User.class);
		userExample.createCriteria().andEqualTo("loginname", loginname);
		return userMapper.selectCountByExample(userExample) == 0;
	}

	@Override
	public User findShowUserById(Integer id) {
		User user = findById(id);
		if (user != null) {
			user.setCardId(null);
			user.setCardName(null);
			user.setPassword(null);
		}
		return user;
	}

	@Override
	public Integer update(User user) {
		User olduser = findById(user.getId());
		if (user.getCardId() == null || user.getCardId().isEmpty()
				|| user.getCardName() == null || user.getCardName().isEmpty()) {
			user.setIspass(User.USER_ISPASS_NOINFO);
		} else {
			if (!user.getCardId().equals(olduser.getCardId())
					|| !user.getCardName().equals(olduser.getCardName())) {
				user.setIspass(User.USER_ISPASS_WAITING);
			}
		}
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public Account findAccountByUserId(Integer userId) {
		Account account = new Account();
		account.setUserId(userId);
		return accountMapper.selectOne(account);
	}

	@Override
	public Account updateAccountPwdByUserId(Integer userId, String newPassword) {
		Account account = findAccountByUserId(userId);
		if (account == null) {
			account = new Account();
			account.setBalance(0.0);
			account.setPayPassword(newPassword);
			account.setUserId(userId);
			accountMapper.insertSelective(account);
		} else {
			account.setPayPassword(newPassword);
			accountMapper.updateByPrimaryKeySelective(account);
		}
		return account;
	}

	@Override
	public Integer updateAccountByUserId(Integer userId, Double money,
			Boolean isAdd) {
		return accountMapper.updateAccountByUserId(userId, money, isAdd);
	}

	@Override
	public Integer updatePwdByUserId(Integer userId, String newPassword) {
		User user = new User();
		user.setId(userId);
		user.setPassword(newPassword);
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public List<User> findWaitingUser(Pagination pagination) {
		User user = new User();
		user.setIspass(User.USER_ISPASS_WAITING);
		return userMapper.selectByRowBounds(user,
				new RowBounds(pagination.getOffset(), pagination.getLimit()));
	}

	@Override
	public Integer countWaitingUser() {
		User user = new User();
		user.setIspass(User.USER_ISPASS_WAITING);
		return userMapper.selectCount(user);
	}

	@Override
	public Integer delete(Integer id) {
		Account account = new Account();
		account.setUserId(id);
		accountMapper.delete(account);
		return userMapper.deleteByPrimaryKey(id);
	}
}
